<html>
<head><meta charset="utf-8"><title>Allocator aware std traits · t-libs/wg-allocators · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/index.html">t-libs/wg-allocators</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20aware.20std.20traits.html">Allocator aware std traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="175826315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20aware%20std%20traits/near/175826315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sydney Acksman <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20aware.20std.20traits.html#175826315">(Sep 16 2019 at 16:03)</a>:</h4>
<p>There's a number of traits that implicitly allocate to the global allocator like <code>str</code> <code>ToOwned</code> making a <code>String&lt;Global&gt;</code> and <code>Clone</code> <code>String&lt;Global&gt;</code> always making another <code>String&lt;Global&gt;</code> which would make it slightly more difficult to move data across allocators.</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">Global</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;hello world!&quot;</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"> </span><span class="c1">// nope, that&#39;s a String&lt;Global&gt;</span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// we could do it manually</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">z</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">z</span><span class="p">.</span><span class="n">push_str</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">as_str</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="n">z</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">clone_in</span><span class="p">(</span><span class="n">System</span><span class="p">);</span><span class="w"> </span><span class="c1">// or use a trait</span>

<span class="kd">let</span><span class="w"> </span><span class="n">s</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;hello world!&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">t</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">Global</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">to_owned</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">u</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">to_owned</span><span class="p">();</span><span class="w"> </span><span class="c1">// nope, it&#39;s Global again</span>
<span class="kd">let</span><span class="w"> </span><span class="n">v</span>: <span class="nb">String</span><span class="o">&lt;</span><span class="n">System</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">to_owned_in</span><span class="p">(</span><span class="n">System</span><span class="p">);</span><span class="w"></span>
</pre></div>


<p>Is there an issue yet talking about traits like this?</p>



<a name="175858139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20aware%20std%20traits/near/175858139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20aware.20std.20traits.html#175858139">(Sep 16 2019 at 22:18)</a>:</h4>
<p>LGTM</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>